package com.example.teachingSystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.teachingSystem.pojo.entity.Semester;
import com.example.teachingSystem.pojo.form.BasePageQuery;
import com.example.teachingSystem.pojo.form.semester.AddSemesterForm;
import com.example.teachingSystem.pojo.form.semester.UpdateSemesterForm;
import com.example.teachingSystem.pojo.vo.OptionVo;
import com.example.teachingSystem.pojo.vo.SemesterVo;
import com.example.teachingSystem.service.SemesterService;
import com.example.teachingSystem.mapper.SemesterMapper;
import com.example.teachingSystem.utils.result.PageResult;
import com.example.teachingSystem.utils.result.Result;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 86147
 * @description 针对表【semester(学期表)】的数据库操作Service实现
 * @createDate 2023-12-10 23:58:05
 */
@Service
public class SemesterServiceImpl extends ServiceImpl<SemesterMapper, Semester>
        implements SemesterService {

    @Autowired
    private SemesterMapper semesterMapper;

    @Override
    public Result<String> addSemester(AddSemesterForm addSemesterForm) {
        Semester semester = new Semester();
        BeanUtils.copyProperties(addSemesterForm, semester);
        this.save(semester);
        return Result.ok(null, "添加成功");
    }


    @Override
    public Result<String> updateSemester(UpdateSemesterForm updateSemesterForm) {
        Semester one = semesterMapper.selectOne(new LambdaQueryWrapper<Semester>()
                .eq(Semester::getId, updateSemesterForm.getId()));
        if (one == null) {
            return Result.build(null, 500, "不存在该学期");
        }
        BeanUtils.copyProperties(updateSemesterForm, one);
        this.updateById(one);
        return Result.ok(null, "修改成功");
    }

    @Override
    public Result<String> deleteSemesterById(Long id) {
        Semester one = semesterMapper.selectOne(new LambdaQueryWrapper<Semester>()
                .eq(Semester::getId, id));
        if (one == null) {
            return Result.build(null, 500, "不存在该学期");
        }
        this.removeById(one);
        return Result.ok(null, "删除成功");
    }

    @Override
    public PageResult<SemesterVo> getSemester(BasePageQuery basePageQuery) {
        Page<Semester> semesterPage = new Page<>(basePageQuery.getPageNum(), basePageQuery.getPageSize());
        IPage<SemesterVo> semesterVoIPage = semesterMapper.getSemesterPage(semesterPage);
        return PageResult.success(semesterVoIPage);
    }

    @Override
    public Result<List<OptionVo>> getSemesterOptions() {
        List<OptionVo> optionVos = semesterMapper.getSemesterOptions();
        return Result.ok(optionVos);
    }

}




